package com.weijin.bookborrowmanagesystem.controller;

import com.weijin.bookborrowmanagesystem.common.Result;
import com.weijin.bookborrowmanagesystem.pojo.Borrow;
import com.weijin.bookborrowmanagesystem.service.BorrowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author WeiJin
 * @Version 1.0
 * @Date 2025/3/13 10:33
 */
@RestController
@RequestMapping("/borrows")
@Api(tags = "图书借阅控制层类")
public class BorrowController {
    @Resource
    private BorrowService borrowService;

    @PostMapping("/borrowBook")
    @ApiOperation("借书")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "bookId", value = "书籍id"),
            @ApiImplicitParam(name = "days", value = "借阅天数")
    })
    public Result<String> borrowBook(@RequestParam("userId") Integer userId, @RequestParam("bookId") Integer bookId,
                                     @RequestParam("days") Integer days) {
        Borrow borrow = new Borrow();
        borrow.setBookId(bookId);
        borrow.setUserId(userId);
        borrow.setDays(days);
        return borrowService.borrowBook(borrow);
    }

    @PostMapping("/returnBook")
    @ApiOperation("还书")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "bookId", value = "书籍id")
    })
    public Result<String> returnBook(@RequestParam("userId") Integer userId, @RequestParam("bookId") Integer bookId) {
        Borrow borrow = new Borrow();
        borrow.setUserId(userId);
        borrow.setBookId(bookId);
        return borrowService.returnBook(borrow);
    }

    @GetMapping("/query/{userId}")
    @ApiOperation("根据用户id获取借阅记录")
    public Result<List<Borrow>> queryByUserId(@PathVariable("userId") Integer userId) {
        return borrowService.queryByUserId(userId);
    }
}
